package com.nightlife.model.rbac;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;


/**
 * 用户组表
 * @author Z.x.Y
 *
 */

@Entity
@Table(name = "SYS_GROUP")
public class Group implements Serializable{
	private static final long serialVersionUID = 1L;
	
	/**
	 * 组ID
	 */
	@Id
	@GeneratedValue
	private int groupId;
	/**
	 * 组名
	 */
	private String groupName;
	
	
	/**
	 * 组/资源关系
	 */
	@ManyToMany
	@ForeignKey(name = "GROUP_RESOURCE_FK", inverseName = "RESOURCE_GROUP_FK")
	@JoinTable(name = "SYS_PRIVILEGE", joinColumns = { @JoinColumn(name = "groupId") }, inverseJoinColumns = { @JoinColumn(name = "resourceId") })
	private Set<Resource> resources;
	
	/**
	 * 用户组/用户关系
	 */
	@ManyToMany(mappedBy="groups")
	private Set<User> users;
	

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public Set<Resource> getResources() {
		return resources;
	}

	public void setResources(Set<Resource> resources) {
		this.resources = resources;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + groupId;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Group other = (Group) obj;
		if (groupId != other.groupId)
			return false;
		return true;
	}
}
